package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.anno.LogAnnotation;
import com.powernode.domain.SysUser;
import com.powernode.service.SysUserService;
import com.powernode.utils.PowerAuthMenuUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/sys/user")
@RestController
@Api(tags = "用户接口管理")
public class SysInfoController {

    @Autowired
    private SysUserService sysUserService;

    @LogAnnotation(operate = "获取当前用户信息")
    @GetMapping("/info")
    @ApiOperation(value = "获取当前用户信息")
    @PreAuthorize("hasAuthority('sys:menu:info')")
    public ResponseEntity<SysUser> sysUserInfo() {
        Long sysUserId = PowerAuthMenuUtil.getSysUserId();
        SysUser sysUser = sysUserService.getById(sysUserId);
        return ResponseEntity.ok(sysUser);
    }


    @GetMapping("/page")
    @ApiOperation(value = "管理员列表")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public ResponseEntity<Page<SysUser>> sysUserPage(Page<SysUser> page, SysUser sysUser) {
     page =  sysUserService.getSysUserPage(page,sysUser);
        return ResponseEntity.ok(page);
    }


    @PostMapping()
    @ApiOperation(value = "管理新增")
    @PreAuthorize("hasAuthority('sys:user:save')")
    public ResponseEntity<Void> addUserAndRole(@RequestBody SysUser sysUser) {
        sysUserService.addUserAndRole(sysUser);
        return ResponseEntity.ok().build();
    }
}
